package com.tencent.qf.mybatis.config;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SSHConnection {
    private Session session;

    public SSHConnection(MysqlSSHProperties mysqlSSHProperties) throws Throwable {
        JSch jsch = new JSch();
        session = jsch.getSession(mysqlSSHProperties.getSshUser(), mysqlSSHProperties.getSshHost(), mysqlSSHProperties.getSshPort());
        session.setPassword(mysqlSSHProperties.getSshPassword());
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();
        session.setPortForwardingL(mysqlSSHProperties.getLocalPort(), mysqlSSHProperties.getMysqlHost(), mysqlSSHProperties.getMysqlPort());
    }

    public void closeSSH() {
        session.disconnect();
    }
}